﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using DotNetUI.Controls.Shapes;

namespace DotNetUI.Controls
{
    /// <summary>
    /// 进度环
    /// </summary>
    public class DotNetProcessRing : Control
    {
        static DotNetProcessRing()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DotNetProcessRing), new FrameworkPropertyMetadata(typeof(DotNetProcessRing)));
        }
        /// <summary>
        /// 最小值
        /// </summary>
        public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register(nameof(MinValue),typeof(double),typeof(DotNetProcessRing),new PropertyMetadata(0.0, PropertyChanged));
        /// <summary>
        /// 最大值
        /// </summary>
        public static readonly DependencyProperty MaxValueProperty=DependencyProperty.Register(nameof(MaxValue),typeof(double),typeof(DotNetProcessRing),new PropertyMetadata(100.0, PropertyChanged));
        /// <summary>
        /// 当前值
        /// </summary>
        public static readonly DependencyProperty ValueProperty=DependencyProperty.Register(nameof(Value),typeof(double),typeof(DotNetProcessRing),new PropertyMetadata(0.0, PropertyChanged));
        /// <summary>
        /// 最小值
        /// </summary>
        public double MinValue { get=>this.GetValue<double>(MinValueProperty); set=>SetValue(MinValueProperty,value); }
        /// <summary>
        /// 最大值
        /// </summary>
        public double MaxValue { get => this.GetValue<double>(MaxValueProperty); set => SetValue(MaxValueProperty, value); }
        /// <summary>
        /// 当前值
        /// </summary>
        public double Value { get => this.GetValue<double>(ValueProperty); set => SetValue(ValueProperty, value); }

        /// <summary>
        /// 环状的宽度
        /// </summary>
        public double RingWidth { get => this.GetValue<double>(RingWidthProperty); set => SetValue(RingWidthProperty, value); }
        /// <summary>
        /// 宽度
        /// </summary>
        public static readonly DependencyProperty RingWidthProperty = DependencyProperty.Register(nameof(RingWidth), typeof(double), typeof(DotNetProcessRing), new PropertyMetadata(10.0));
        private static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is DotNetProcessRing dotNetProcess)
            {

                dotNetProcess.OnValueChanged();
            }
        }
        /// <summary>
        /// 值被修改事件
        /// </summary>
        public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(nameof(ValueChanged), RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(DotNetProcessRing));
        /// <summary>
        /// 当值被修改时触发
        /// </summary>
        public event EventHandler<RoutedEventArgs> ValueChanged
        {
            add 
            {
                AddHandler(ValueChangedEvent,value);
            }
            remove
            {
                RemoveHandler(ValueChangedEvent,value);
            }
        }
        /// <summary>
        /// 值被修改了
        /// </summary>
        protected virtual void OnValueChanged()
        {
            if (MaxValue>MinValue&&Value<=MaxValue&&Value>=MinValue)
            {
                var args = new RoutedEventArgs(ValueChangedEvent, this);
                this.RaiseEvent(args);
            }
        }
        /// <summary>
        /// 值渲染模板
        /// </summary>
        public static readonly DependencyProperty ValueTemplateProperty = DependencyProperty.Register(nameof(ValueTemplate),typeof(DataTemplate),typeof(DotNetProcessRing));
        /// <summary>
        /// 值渲染模板
        /// </summary>
        public DataTemplate ValueTemplate { get=>this.GetValue<DataTemplate>(ValueTemplateProperty); set=>SetValue(ValueTemplateProperty,value); }
    }
}
